Flink.cor <- function(crds,cmat,latlong=F,basicpch=1,basiccex=.5) { # Function to produce and link geographic scatter and variogram # (or covariance or correlation vs distance) plot # crds: monitoring site coordinates: rectangular coordinates or (lat,long) # cmat: nxn matrix of covariances, correlations, or semivariances # latlong: indicator for whether crds are (lat,long) or not (rectangular). # Two graphics windows will be opened; reposition one in order to see both. # Make sure that the "maps" package is loaded. #library("maps") if(is.null(dimnames(crds))) dimnames(crds) <- list(1:nrow(crds),c("x","y")) if (latlong) crds <- crds[,c(2,1)] imore <- T while (imore) { # Open two windows and get their device numbers windows() w1 <- dev.list()[length(dev.list())] windows() w2 <- dev.list()[length(dev.list())] # Create geographic scatter dev.set(w1) if (latlong) { map("county",col=3,xlim=range(crds[,1]),ylim=range(crds[,2])) dmat <- dist2full(dist(Flamb2(crds[,c(2,1)])$xy)) } else { plot(crds[,1],crds[,2],type="n") dmat <- dist2full(dist(crds)) } points(crds[,1],crds[,2]) text(crds[,1],crds[,2],dimnames(crds)[[1]],adj=0) # Create scatter of distance vs [correlation/covariance/semivariance] dev.set(w2) plot(dmat,cmat,pch=basicpch,cex=basiccex) pairlab <- paste(row(cmat),col(cmat),sep=",") junk2 <- 0 junk1 <- 0 cat("\nIdentify one or more points on Distance-Correlation plot to be linked with the geographic plot.\n") #Then, if desired, identify points on the Geographic plot #to be linked with the Distance-Correlation plot.\n") icol <- 0 while( length(junk1) > 0 ) { icol <- icol+1 dev.set(w2) junk1 <- identify(dmat,cmat,pairlab,n=1, col=icol,cex=.75) if(length(junk1)>0) { ix <- c(row(cmat))[junk1] iy <- c(col(cmat))[junk1] dev.set(w1) segments(crds[ix,1],crds[ix,2],crds[iy,1],crds[iy,2],lwd=2, col=icol) } } dev.set(w1) cat("\nIdentify one or more points on the Geographic plot to be linked with the Distance-Correlation plot.\n") cat("Identify two different sites or select the same site twice to identify all of its pairs in the Distance-Correlation plot.\n") icol <- 0 junk2 <- array() siteid1 <- 0 siteid2 <- 0 while( length(siteid1)>0 ) { dev.set(w1) icol <- icol+1 siteid1 <- identify(crds[,1],crds[,2],n=1, col=icol,cex=.75) if (length(siteid1)>0) { siteid2 <- identify(crds[,1],crds[,2],n=1, col=icol,cex=.75) junk2[icol] <- siteid1 dev.set(w2) if (siteid1==siteid2) { for (j in (1:nrow(crds))[-junk2[icol]]) { points(dmat[junk2[icol],j],cmat[junk2[icol],j], col=icol,pch=16) text(dmat[junk2[icol],j],cmat[junk2[icol],j], paste(junk2[icol],",",j,sep=""),adj=0,cex=.75) } } else { points(dmat[siteid1,siteid2],cmat[siteid1,siteid2], col=icol,pch=16) text(dmat[siteid1,siteid2],cmat[siteid1,siteid2], paste(siteid1,",",siteid2,sep=""),adj=0,cex=.75) } } } dev.set(w2) legend(x="topright",pch=rep(16,(icol-1)),col=(1:(icol-1)),legend=junk2) cat("\nContinue with another pair of windows? (enter 'T' or 'F')\n") imore <- scan(what=logical(),n=1) } }